home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Swar / debris.c next >
Text File  |  1994-08-21  |  2KB  |  83 lines

  1.  
  2. #include "swar.h"
  3.  
  4. InitDebris()
  5. {
  6.     extern DEBRISREC        gDebrisRecs[MAX_DEBRIS], gOldDebrisRecs[MAX_DEBRIS];
  7.     short                    i;
  8.     
  9.     for (i = 0; i < MAX_DEBRIS; i++) {
  10.         gDebrisRecs[i].where.v = -1;
  11.         gDebrisRecs[i].where.h = -1;
  12.         gDebrisRecs[i].vel.v = -1;
  13.         gDebrisRecs[i].vel.h = -1;
  14.         gDebrisRecs[i].countdown = 0;
  15.     } /* for */
  16.  
  17. } /* InitDebris() */
  18.  
  19. CreateDebris(number, tics, h, v, hv, vv)
  20.     short        number, tics, h, v, hv, vv;
  21. {
  22.     short        i;
  23.     
  24.     for (i = 0; i < MAX_DEBRIS; i++)
  25.         if (gDebrisRecs[i].countdown == 0) {
  26.             gDebrisRecs[i].where.v = v + RngRnd(-3, 3);
  27.             gDebrisRecs[i].where.h = h + RngRnd(-3, 3);
  28.             gDebrisRecs[i].vel.v = vv + RngRnd(-3, 3);
  29.             gDebrisRecs[i].vel.h = hv + RngRnd(-3, 3);
  30.             gDebrisRecs[i].countdown = tics;
  31.             gDebrisRecs[i].color.red = RngRnd(0, 256) * 256;
  32.             gDebrisRecs[i].color.green = RngRnd(0, 256) * 256;
  33.             gDebrisRecs[i].color.blue = RngRnd(0, 256) * 256;
  34.             if (--number == 0)
  35.                 return;
  36.         } /* if */
  37.         
  38.     
  39. } /* CreateDebris() */
  40.  
  41. MoveDebris()
  42. {
  43.     short                    i;
  44.     
  45.     for (i = 0; i < MAX_DEBRIS; i++)
  46.         if (gDebrisRecs[i].countdown) {
  47.             gDebrisRecs[i].countdown--;
  48.             gDebrisRecs[i].where.h += gDebrisRecs[i].vel.h;
  49.             gDebrisRecs[i].where.v += gDebrisRecs[i].vel.v;
  50.             if (gDebrisRecs[i].where.v < 0)
  51.                 gDebrisRecs[i].vel.v *= -1;
  52.             if (gDebrisRecs[i].where.v > 479)
  53.                 gDebrisRecs[i].vel.v *= -1;
  54.             if (gDebrisRecs[i].where.h < 0)
  55.                 gDebrisRecs[i].vel.h *= -1;
  56.             if (gDebrisRecs[i].where.h > 639)
  57.                 gDebrisRecs[i].vel.h *= -1;
  58.         } /* if */
  59.     
  60. } /* MoveDebris() */
  61.  
  62. DrawDebris()
  63. {
  64.     short                hloc;
  65.     extern short        gXv;
  66.     short                i;
  67.     GrafPtr                savePort;
  68.     extern CGrafPort    *gOSPtr;
  69.     extern RGBColor    myBlack;
  70.     
  71.     GetPort(&savePort);
  72.     SetPort((GrafPtr)gOSPtr);
  73.     for (i = 0; i < MAX_DEBRIS; i++) {
  74.         if (gOldDebrisRecs[i].countdown)
  75.             SetCPixel(gOldDebrisRecs[i].where.h, gOldDebrisRecs[i].where.v, &myBlack);
  76.         if (gDebrisRecs[i].countdown)
  77.             SetCPixel(gDebrisRecs[i].where.h, gDebrisRecs[i].where.v, &(gDebrisRecs[i].color));
  78.         gOldDebrisRecs[i] = gDebrisRecs[i];
  79.     } /* for */
  80.     SetPort(savePort);
  81.                         
  82. } /* DrawDebris() */
  83.